<template>
    <div>
        <div style="display: flex;">
			
            <div style="margin-right:20px;">
                
				<el-button @click="newAdd" size="mini" type="primary">新增</el-button>
				
				<el-button  @click="saveOrder" 
					:loading="loading_btn"
					size="mini" type="primary">保存</el-button>
				<el-button size="mini" @click="prevNextBtn(1)" type="primary">上一条</el-button>
				<el-button size="mini" @click="prevNextBtn(2)" type="primary">下一条</el-button>
            </div>
			<div style="margin-top: 2px;" v-if="form.ttype==3 && !isOutDepDis">
				<el-checkbox v-model="isOutDep"
					:disabled="isOutDepDis"
					border
					size="mini">释放押金</el-checkbox>
			</div>
			<!-- <div style="margin-top: 2px;">
				<el-checkbox v-model="orderChecked" border
					size="mini">保存后新增</el-checkbox>
			</div> -->
			
        </div>
        <br>
        <div class="searchForm" style="display: block;">
			<div class="flex">
				<div style="margin-right: 10px;">
				    订单ID： 
					<el-input size="small" v-model="order"
					placeholder="请输入单号" disabled
					style="width:150px;"></el-input>
				</div>
				<div style="margin-right: 10px;" v-if="form.order_sn">
				    订单编号： 
					<el-input size="mini" v-model="form.order_sn"
					disabled
					placeholder=""
					style="width:170px;"></el-input>
				</div>
				<div style="margin-right: 10px;">
				    采购日期： 
					<el-date-picker
					      size="mini"
					      v-model="buyer_time"
						  value-format="yyyy-MM-dd"
					      type="date"
					      placeholder="选择日期时间">
					    </el-date-picker>
				</div>
				<div style="margin-right: 10px;">
					<div style="display: flex;">
						<div>
							供应商：
						</div>
						<div>
							<!-- <selectSupplier
								:supp="form.supplier" 
								@getSupplierName="getSupplierName" /> -->
							<el-input size="mini" v-model="form.supplier"
							disabled
							placeholder=""
							style="width:170px;"></el-input>
						</div>
					</div>
				</div>
				<div style="margin-right: 10px;">
				    物流单号： <el-input size="mini" 
					v-model="delivery_number" 
					@blur="getDeclarationInfo"
					@keyup.enter.native="getDeclarationInfo"
					placeholder="请输入物流单号"  
					style="width:150px;"></el-input>
				</div>
				<div>
				    跑腿ID： <el-input size="mini" 
					v-model="pao_id" 
					@blur="getDeclarationInfo"
					@keyup.enter.native="getDeclarationInfo"
					placeholder="请输入跑腿ID"  
					style="width:150px;"></el-input>
				</div>
			</div>
			<div class="flex">
				<!-- <div style="margin-right: 10px;" v-if="form.ttype==3">
				    定价品类： 
					<el-input size="mini" v-model="form.goods_name"
					disabled
					placeholder="请输入定价品类"
					style="width:150px;"></el-input>
				</div> -->
				<div style="margin-right: 10px;" v-if="form.ttype==3">
				    锁定单价： 
					<el-input size="mini" v-model="form.pre_price"
					disabled
					placeholder="请输入锁定单价"
					style="width:150px;"></el-input>
				</div>
				
				<div style="margin-right: 10px;" v-if="form.ttype==3">
				    定价克重： 
					<el-input size="mini" v-model="form.pre_weight"
					disabled
					placeholder="请输入定价克重"
					style="width:150px;"></el-input>
				</div>
				<div style="margin-right: 50px;" v-if="form.ttype==3 && order=='new'">
				    已入克重： 
					<!-- <el-input size="mini" v-model="form.show_weight"
					disabled
					placeholder="请输入已入克重"
					style="width:150px;"></el-input> -->
					<span v-if="now_show_weight">
						{{form.show_weight}} + {{now_show_weight}} = 
						{{(form.show_weight*1+now_show_weight*1).toFixed(2)}}
					</span>
					<span v-else>
						{{form.show_weight}}
					</span>
				</div>
				<div style="margin-right: 10px;" >
				    入库类型： 
					<el-select 
						disabled
						size="mini" v-model="form.ttype" placeholder="请选择">
					    <el-option
					      v-for="item in options"
					      :key="item.value"
					      :label="item.label"
					      :value="item.value">
					    </el-option>
					</el-select>
				</div>
			</div>
        </div>
        <div style="margin: 15px 0;" class="flex">
			<el-button  @click="addBtn" size="mini" type="warning">增加行</el-button>
			<!-- <el-button @click="delLastList"  size="mini" type="warning">删除后行</el-button> -->
			<div style="margin-left: 10px;" v-if="form.ttype==1 || form.ttype==2">
			    实时单价： 
				<el-input size="mini" v-model="now_price"
				disabled
				placeholder=""
				style="width:150px;"></el-input>
			</div>
		</div>
        <el-table  :data="list"  
			 border  
			>
			
			<el-table-column   align="center"  width="80"
				label="序号">
				<template slot-scope="scope">
				   <div>
						{{scope.$index+1}}
				   </div>
				</template>
			</el-table-column>	
			<el-table-column   align="center" width="80"
				label="操作">
				<template slot-scope="scope">
				   <i  class="el-icon-delete" 
					v-if="!scope.row.pao_id"
					style="cursor: pointer;"
				   @click.stop="delRow(scope)"></i>
				   <i v-else>
					   --
				   </i>
				</template>
			</el-table-column>
			<el-table-column label="ID" align="center" 
				width="80px" prop="pao_id" />
            <el-table-column   align="center"  width="500px" 
				label="实收品类">
				<template slot-scope="scope">
				   <div v-if="!scope.row.id" class="flex">
						<selectGoods  v-if="isShowSelectGoods" 
							:ref="'refGoods'+scope.$index"
							:scopeData="scope" 
							@getGoodsData="getGoodsData" />
						<div v-if="scope.row.pao_id" 
							style="margin-left: 5px;margin-top: 3px;"
							>
							<span class="button_text" @click="uplodeImg(scope)">上传图片</span>
						</div>
				   </div>
				   <div v-else>
					   <el-input size="mini" 
						disabled
						v-model="scope.row.show_goods_name"></el-input>
				   </div>
				   
				   
				   
				</template>
			</el-table-column>
			<!-- 1 行情卖料 2灵活寄存 3 定价卖料 -->
			<el-table-column width="130px"  
				v-if="form.ttype==1"
				align="center"
				label="实收单价">
				<template slot-scope="scope">
				   <div>
						<el-input
						 size="mini"
						  type="number"
						 @input="comp_pay_money(scope)"
						 v-model="scope.row.show_price"></el-input>
				   </div>
				</template>
			</el-table-column>
			<el-table-column width="130px"  align="center"
				label="实收重量">
				<template slot-scope="scope">
				   <div>
						<el-input
						 size="mini"
						  type="number"
						 @input="comp_pay_money(scope)"
						 v-model="scope.row.show_weight"></el-input>
				   </div>
				</template>
				<!-- @blur="comp_pay_money(scope)" -->
			</el-table-column>
            <el-table-column  align="center" width="130px"  
				v-if="form.ttype!=2"
				label="实际结算金额">
				<template slot-scope="scope">
				   <div>
						<el-input 
							size="mini"
						v-model="scope.row.pay_money"></el-input>
				   </div>
				</template>
			</el-table-column>
			<!-- <el-table-column   align="center"  width="120px"  label="跑腿id">
				<template slot-scope="scope">
				   <div>
						<el-input size="mini"
							type="number"
							v-model="scope.row.pao_id"></el-input>
				   </div>
				</template>
			</el-table-column> -->
            <el-table-column   align="center"  width="130px"   label="备注">
				<template slot-scope="scope">
				   <div>
						<el-input size="mini" v-model="scope.row.remark"></el-input>
				   </div>
				</template>
			</el-table-column>
		  
        </el-table>
		
		<el-dialog
		  title="上传图片"
		  :close-on-click-modal="false"
		  @close="closeUplodeImg"
		  :visible.sync="isShowUplodeImg"
		  width="700px">
		  <el-form ref="form" :model="form" label-width="120px">
		  	<el-form-item label="商品名称:">
		  	    <el-input  disabled v-model="rowData.show_goods_name" size="small"></el-input>
		  	</el-form-item>
			<el-form-item label="鉴定前二维码:">
				<div id="d1" style="width: 100px;" v-if="isShowUplodeImg" />
			</el-form-item>
			<el-form-item label="鉴定前图片:">
				<div style="display: flex;">
				  <div style="position: relative;margin-right: 10px;" 
					v-for="(el,i) in imgUrlList1" :key="i">
					  <!-- <img style="width: 100px;height: 100px;" :src="el.img_url" alt=""> -->
					  <el-image style="width: 100px; height: 100px"
						  :preview-src-list="[el.img_url]"
						  :src="el.img_url"></el-image>
					  <i @click="delImg(el,i)" class="el-icon-circle-close" style="font-size: 20px;position: absolute;top: 0;right:0;cursor: pointer;"></i>
				  </div>
				</div>
			</el-form-item>
			<el-form-item label="鉴定后二维码:">
				<div id="d2" style="width: 100px;" v-if="isShowUplodeImg" />
			</el-form-item>
			<el-form-item label="鉴定后图片:">
				<div style="display: flex;">
				  <div style="position: relative;margin-right: 10px;" 
					v-for="(el,i) in imgUrlList2" :key="i">
					  <!-- <img style="width: 100px;height: 100px;" :src="el.img_url" alt=""> -->
					  <el-image style="width: 100px; height: 100px"
						  :preview-src-list="[el.img_url]"
						  :src="el.img_url"></el-image>
					  <i @click="delImg(el,i)" class="el-icon-circle-close" style="font-size: 20px;position: absolute;top: 0;right:0;cursor: pointer;"></i>
				  </div>
				</div>
			</el-form-item>
		  </el-form>	
		</el-dialog>
		
    </div>
</template>
<script>
	
	import selectGoods from "@/components/selectGoods"
	import selectSupplier from "@/components/selectSupplier"
	import mixin from './mixin/index.js'
    export default {
        components:{
          selectGoods,selectSupplier
        },
		computed:{
			order() {
			  let str = 'new'
			  if(this.order_number){
				 str = this.$utils.sliceStr(this.order_number,6)
			  }
			  return str
			}
		},
		
		mixins:[mixin],
        data(){
            return {
				isOutDepDis:false,
				order_number:'',
				loading_btn:false,
				isShowSelectGoods:true,
                list:[],
				orderChecked:true,
				buyer_time:'', // 入库时间
				delivery_number:'', // 物流单号
				pao_id:'', // 跑腿id
				now_price:'请输入实收品类', // 实时单价
				isOutDep:false, // 是否释放押金
				form:{
					order_sn:'',
					supplier:'', //供应商
					goods_name:'', // 定价品类
					pre_price:'', // 锁定单价
					pre_weight:'', // 定价克重
					show_weight:'', // 已入克重
					ttype:'', // 1 行情卖料 2灵活寄存 3 定价卖料 
					mid:0
				},
				timer:null,
				now_show_weight:'',
            }
        },
		beforeRouteLeave(to,from,next){
			this.$notify.closeAll()
			clearInterval(this.timer)
			next()
		},
        mounted(){
			this.buyer_time = this.$utils.formatNowDate(2)
			this.timer = setInterval(()=>{
				let goods_type_name = this.list[0].show_goods_name
				this.getGoldLoopData(goods_type_name)
			},1000)
			let order_number = this.$route.query.order_number
			if(order_number){
				this.initData()
			}else{
				this.addBtn()
			}
			
		},
        methods:{
			isOutDepBtn(){
				let params = {
					order_sn:this.form.order_sn
				}
				this.$post('/depositSys/isOutDep',params,res=>{
					let result = res.data
					if(result.code==200){
						this.isOutDepDis = true
					}
				})
			},
			getGoldLoopData(goods_type_name){
				if(this.form.ttype!=1 && this.form.ttype!=2) return
				if(!goods_type_name) return 
				let params = {
					isLoading:0,
					goods_type_name:goods_type_name
				}
				this.$post('/homeSet/getGoldLoopData',params,res=>{
					let gold_price = res.data.data.gold_price
					let arr = []
					for(let i in res.data.data.items){
						arr.push(res.data.data.items[i])
					}
					
					let filterData = arr.filter((el,i)=>{
						return el.code == gold_price.goods_code
					})
					let dd = filterData[0]
					let obj = {
						re_price:(dd.bidprice*1+gold_price.float_re_price*1).toFixed(2), // 回购价格
						sael_price:(dd.askprice*1+gold_price.float_sael_price*1).toFixed(2)    // 销售价格
					}
					this.now_price = obj.re_price
				})
			},
			//查找下一条上一条
			prevNextBtn(handerType=1){
				
				let params = {
					order_number:this.$route.query.order_number || -1,
					handerType:handerType,
				}
				this.$post('/buyerSys/prevNextBtn',params,res=>{
					let result = res.data
					if(result.code==200){
						let order_number = result.data.order_number_new
						if(order_number){
							this.$router.replace({
								path:this.$route.path,
								query:{
									order_number:order_number,
								}
							})
							this.initData()
						}
					}
					
				})
			},
			initData(){
				let order_number = this.$route.query.order_number
				if(order_number){
					this.list = []
					this.$post('/buyerSys/getBuyerForOrder',{order_number},res=>{
						let result = res.data
						if(result.data.length){
							this.delivery_number = result.data[0].delivery_number
							this.form.supplier = result.data[0].supplier
							this.form.ttype = result.data[0].ttype		
							this.form.order_sn = result.data[0].order_sn
							this.order_number = result.data[0].order_number
							if(result.data[0].ttype==3){
								this.form.pre_price = result.data[0].pre_price
								this.form.pre_weight = result.data[0].pre_weight
								this.isOutDepBtn()
							}
							for(let item of result.data){
								this.list.push(
									{
										pao_id:item.pao_id,
										id:item.id,
										show_goods_name:item.show_goods_name, // 收货商品名称
										pay_money:item.pay_money,  // 实际结算金额
										show_weight:item.show_weight, // 实收重量
										show_price:item.show_price, // 实收重量
										remark:item.remark,
									}
								)
							}
							
						}
						
						// delivery_number
						// form.supplier
						// ttype
						
						// form:{
						// 	order_sn:'',
						// 	supplier:'', //供应商
						// 	goods_name:'', // 定价品类
						// 	pre_price:'', // 锁定单价
						// 	pre_weight:'', // 定价克重
						// 	show_weight:'', // 已入克重
						// 	ttype:'', // 1 行情卖料 2灵活寄存 3 定价卖料 
						// 	mid:0
						// },
						console.log(result,3333333)
						// if(result.data.length){
						// 	this.form.buyer_date = result.data[0].buyer_date
						// 	this.form.buyer_person = result.data[0].buyer_person
						// 	this.form.supplier = result.data[0].supplier
						// 	this.form.logistics_number = result.data[0].logistics_number
						// 	for(let item of result.data){
						// 		let obj = {
						// 			id:item.id,
						// 			goods_name:item.goods_name,
						// 			goods_number:item.goods_number,
						// 			price:item.price,
						// 			money:item.goods_number * item.price,
						// 			codeArr:JSON.parse(item.code_arr),
						// 			remark:item.remark,
						// 			order_number:item.order_number,
						// 			brand:item.brand,
						// 			goods_type:item.goods_type,
						// 			initial_price:item.initial_price,
						// 			is_disabld:item.is_disabld,
						// 			pay_order_number:item.pay_order_number,
						// 			is_settle_accounts:item.is_settle_accounts,
						// 			is_return:item.is_return
						// 		}
						// 		this.list.push(obj)
						// 	}
							
						// }
					})
				}
				
			},
			comp_pay_money(scope){
				let index = scope.$index
				this.now_show_weight = this.list[index].show_weight
				if(this.form.ttype==3){
					this.list[index].pay_money = (this.form.pre_price*this.list[index].show_weight).toFixed(2)
				}else{
					if(this.list[index].show_price && this.list[index].show_weight){
						this.list[index].pay_money = (this.list[index].show_price*this.list[index].show_weight).toFixed(2)
					}
				}
				
			},
			getSupplierName(val){
				this.form.supplier = val
			},
			getGoodsData(val){
				let index = val.rowIndex
				this.list[index].show_goods_name = val.goods_name
			},
			newAdd(){
				if(this.$route.query.order_id){
					this.$router.replace({
						path:this.$route.path,
						query:{
							
						}
					})
				}
				
				this.list = []
			
				this.delivery_number='' // 物流单号
				this.now_price='' // 实时单价
				this.now_show_weight = ''
				this.pao_id = ''
				this.form={
					order_sn:'',
					supplier:'', //供应商
					goods_name:'', // 定价品类
					pre_price:'', // 锁定单价
					pre_weight:'', // 定价克重
					show_weight:'', // 已入克重
					ttype:'', // 1 行情卖料 2灵活寄存 3 定价卖料 
					mid:0
				}
				this.$notify.closeAll()
				this.addBtn()
			},
			saveOrder(){
				if(!this.form.order_sn) return this.$message.error('订单编号不能为空')
				if(!this.form.ttype) return this.$message.error('请选择入库类型')
				
				let order_number = this.$utils.generateRandomString(2)
				
				this.list = this.list.filter((item)=>{
					return item.show_goods_name
				})
				
				// 定价卖料情况
				for(let item of this.list){
					item.order_sn = this.form.order_sn
					
					item.ttype = this.form.ttype
					item.buyer_time = this.buyer_time
					if(this.form.ttype==3){
						item.show_price = this.form.pre_price
						// item.pao_id = this.pao_id
					}
					if(this.$route.query.order_number){
						item.order_number = this.$route.query.order_number
					}else{
						item.order_number = order_number
					}
					
					if(this.form.ttype==2){
						item.status='0'
						item.show_price = ''
						item.pay_money = ''
					}else{
						item.pay_time = this.buyer_time
					}
					item.supplier = this.form.supplier
					item.mid = this.form.mid
					item.delivery_number = this.delivery_number
					
				}
			
				this.loading_btn = true
				this.$post('/buyerSys/addEditBuyer',this.list,res=>{
					this.loading_btn = false
					this.$message.success('操作成功')
					// 释放押金
					if(this.isOutDep){
						if(!this.form.order_sn) return this.$message.error('没有订单编号不能释放押金')
						let order_sn = this.form.order_sn
						this.$post('/buyerSys/outDep',{order_sn},res=>{
							
						})
					}
					this.newAdd()
					
					
				})
			},
			getDeclarationInfo(){
				let params = {
					id:'',
					delivery_number:this.delivery_number,
					pao_id:this.pao_id
				}
			    
				if(!params.delivery_number && !params.pao_id){
					return
				}
				
				this.$post('/order/getBdOrder',params,res=>{
					let result = res.data.data
					this.$notify.closeAll()
					if(result){
						if(result.result2){
							if(result.result2.length){
								this.initData2(result.result2)
								this.form.ttype = result.result2[0].ttype
							}
						}else if(result.result3){
							this.initData1(result)
							this.form.ttype = '3'
						}
					}
				})
			},
            
        }
    }
</script>
